<?php


class menu_model extends CI_Model
{
    private $_table;
    public function __construct() {
        parent::__construct();
        $this->_table = 'menu_code';
    }
    
    
    public function add($list)
    {
        if(!is_array($list))
            $list = array($list);
        $ret = true;
        foreach ($list as $l)
            $ret = $ret && $this->db->insert( $this->_table  , $l);
        return $ret;
    }
    
    public function get_all()
    {
        return  $this->db->order_by('code' ,"ASC" )
        ->from($this->_table)->get()->result_array();
        
                
    }
    public function get_by($data)
    {
        $query = $this->db->from($this->_table)
                 ->where($data[0],$data[1])
                ->get();
        return $query->result_array();
    }
    
    function update($row_id , $data)
    {
        return $this->db->set('title' , $data['title'])
                ->where('id',$row_id)
                ->update($this->_table);
        
    }
    function truncat()
    {
        return $this->db->query('DELETE FROM '.$this->_table);
    }
    
    // ---------------------- Gestion du map des images -------------------//
    public function delete_maps($img = "ALL"){
    	if($img === "ALL"){
    		return $this->db->query('DELETE FROM imageMap');
    	}
    	return $this->db->delete('imageMap', array('img_id' => $img)); ;
    }
    
    // inserer un ensemble de coordonne relative a un image
    public function insert_img_coords($img_id , $coords){
    	
    	// supression des coordonnes déja existatnt 
    	$this->delete_maps($img_id);
    	// insertion des nouveaux coordonnes 
    	$data = array();
    	/*
    	 * img_id	titre	url	coords	description
    	 */
    	foreach($coords as $rect_id => $prop)
    	{
    		$data[] = array(
    				'img_id' 		=> $img_id,
    				'title'	 		=> $prop['title'],
    				'url'			=> $prop['url'],
    				'url_extern'	=> ( $prop['extern'] )? 1 : 0,
    				'coords'		=> $prop['coords'],
    				'description'	=> htmlspecialchars( $prop['desc'] ),
    				'type'			=> $prop['type'],
    		);
    	}
    	return $this->db->insert_batch('imageMap', $data);
    }
    
    public function getMapbyImgName($img_name){

    	$query = $this->db->select('imageMap.img_id, imageMap.title, imageMap.url, imageMap.url_extern, imageMap.coords, imageMap.description')
    				  ->from('menu_code')
    				  ->join('imageMap' , 'menu_code.id = imageMap.img_id')
    				  ->where('menu_code.code'  ,$img_name )
    				  ->get();
    	return $query->result_array();
    }
    
    
}
